package com.zx.club.oss.util;

import com.zx.club.oss.entity.FileInfo;
import io.minio.*;
import io.minio.http.Method;
import io.minio.messages.Bucket;
import io.minio.messages.Item;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;


public class MinioUtil {

    private MinioClient minioClient;

    public MinioUtil(MinioClient minioClient){
        this.minioClient = minioClient;
    }

    /**
     * 创建 bucket 桶
     *
     * @param bucketName 桶名称
     * @throws Exception 创建失败抛出异常
     */
    public void createBucket(String bucketName) throws Exception {
        boolean exists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
        if (!exists) {
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
        }
    }


    /**
     * 上传文件
     *
     * @param inputStream 文件输入流
     * @param bucketName  桶名称
     * @param objectName  文件名称
     * @throws Exception 上传文件异常
     */
    public void uploadFile(InputStream inputStream, String bucketName, String objectName) throws Exception {
        minioClient.putObject(
                PutObjectArgs.builder()
                        .bucket(bucketName)
                        .object(objectName)
                        .stream(inputStream, -1, Integer.MAX_VALUE)
                        .build());
    }


    /**
     * 列出所有桶
     *
     * @return 桶名称列表
     * @throws Exception 获取桶的异常
     */
    public List<String> getAllBucket() throws Exception {
        return minioClient.listBuckets().stream().map(Bucket::name).toList();
    }


    /**
     * 列出桶下的所有文件
     *
     * @param bucketName 桶名称
     * @return 文件信息列表
     * @throws Exception 获取文件信息异常
     */
    public List<FileInfo> getAllFile(String bucketName) throws Exception {
        Iterable<Result<Item>> results = minioClient.listObjects(ListObjectsArgs.builder().bucket(bucketName).build());
        List<FileInfo> fileInfoList = new LinkedList<>();

        for (Result<Item> result : results) {
            Item item = result.get();
            String fileName = item.objectName();
            boolean dir = item.isDir();
            String etag = item.etag();

            fileInfoList.add(new FileInfo(fileName, dir, etag));
        }

        return fileInfoList;
    }

    /**
     * 下载文件
     *
     * @param bucketName 桶名称
     * @param objectName 文件名称
     * @return 文件流
     * @throws Exception 下载失败抛出异常
     */
    public InputStream downloadFile(String bucketName, String objectName) throws Exception {
        return minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(objectName).build());
    }

    /**
     * 删除桶下的文件
     *
     * @param bucketName 桶名称
     * @param objectName 文件名称
     * @throws Exception 删除失败抛出异常
     */
    public void deleteFile(String bucketName, String objectName) throws Exception {
        minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucketName).object(objectName).build());
    }

    /**
     * 删除桶
     *
     * @param bucketName 桶名称
     * @throws Exception 删除失败抛出异常
     */
    public void deleteBucket(String bucketName) throws Exception {
        minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucketName).build());
    }

    public String getUrl(String bucketName, String objectName)throws Exception {
        GetPresignedObjectUrlArgs args = GetPresignedObjectUrlArgs.builder()
                .bucket(bucketName)
                .object(objectName)
                .method(Method.GET).build();
        return minioClient.getPresignedObjectUrl(args);
    }

}
